// pages/project/project.js
const util = require('../../utils/util.js')
const app = getApp();
Page({
    data: {
        url: '',
        swiperCurrent: 0,
        question: [],
        swipp: false,
        name: '',
        docname: [],
        assetTotal:'',
        toAuditTotal:''
    }, 
    onShow() {
        this.data.name = wx.getStorageSync('name')
        var json = []
        var uid = wx.getStorageSync('id')
        var uname = wx.getStorageSync('name')

        wx.request({
          url: `${app.globalData.api}/wx/showAssets?id=`+wx.getStorageSync('id'), // 假设后端接口地址
          method: 'GET', // 根据后端接口类型调整（GET/POST等）
          header: {
            'content-type': 'application/json', // 通用请求头
            // 如需认证可添加Token
            // 'Authorization': `Bearer ${app.globalData.token}`
          },
          success: (res) => {
            if (res.statusCode === 200 ) {
              const assets = res.data.data || []; // 解析后端返回数据结构
              this.setData({
                assetTotal: assets.length
              });
            } else {
              this.handleRequestError(res.data.msg || '获取资产失败');
            }
          },
          fail: (err) => {
            this.handleRequestError('网络请求失败，请检查网络连接');
            console.error('资产数据请求失败:', err);
          },
          complete: () => {
            this.setData({ isLoading: false }); // 隐藏加载状态
          }
        });


        wx.request({
          url: `${app.globalData.api}/wx/myAssets?id=`+wx.getStorageSync('id'), // 替换为实际接口URL`${app.globalData.api}/wx/myAssets?id=`+wx.getStorageSync('id')
          success: (res) => {
            console.log(res.data.data)
            this.setData({
              toAuditTotal:  res.data.data.length
          })
        
          },
          fail: () => {
            wx.showToast({ title: '获取数据失败', icon: 'none' });
          }
        });
       
        // console.log(uid+'index')
        // console.log(uname+'name')
        // util.http('/wx/note?uid=' + uid, resp => {
        //     var json = []
        //     for (var bean of resp.visitBeans) {
        //         var item = {}
        //         item.name = bean.vdoctorName
        //         item.note = bean.vnote
        //         item.status = bean.vstatus
        //         item.ctime = bean.vctime
        //         json.push(item)
        //     }
        //     console.log("123---"+json.name)
        //     this.data.question = json
        //     this.setData(this.data)
        // })
        // util.http('/wx/doctor', resp => {
        //     this.data.tempData = resp.doctorBeans
        //     console.log(resp.doctorBeans)
        //     this.setData(this.data)
        // })
    },
    swiperChange: function (e) {
        this.setData({
            swiperCurrent: e.detail.current
        })
    },
    scanQRCode() {
      wx.scanCode({
        success: (res) => {
          wx.showToast({
            title: `扫描结果：${res.result}`,
            icon: 'none'
          });
          // 这里可以添加解析二维码内容、资产关联等业务逻辑
          util.http('/wx/addAssetsByCode', res.result, resp => {
            console.log(resp)
            if (resp.code == 200) {
              util.alert('入库成功')
              } else {
                  util.alert(resp.msg)
              }
          })
          console.log('扫描结果：', res.result);
        },
        fail: (err) => {
          wx.showToast({
            title: '扫描失败',
            icon: 'none'
          });
          console.error('扫描错误：', err);
        }
      });
    },navigateToAssetList(){
      wx.navigateTo({
        url: '/pages/assetList/assetList?id=123' 
      })
    },navigateToAddAsset(){
      wx.navigateTo({
        url: '/pages/add-asset/add-asset?id=123' 
      })
    },navigateToAssetReport(){
      wx.navigateTo({
        url: '/pages/report/report?id=123' 
      })
    }
    
})